package com.zxh.blog.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxh.blog.domain.Role;
import com.zxh.blog.domain.User;
import com.zxh.blog.service.RoleService;
import com.zxh.blog.service.UserService;
import com.zxh.blog.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

@Controller
@RequestMapping("/user")
public class AdminUserController {

	@Autowired
	UserService userService;

	@Autowired
	RoleService roleService;

	@RequestMapping(value = "/index",method = RequestMethod.GET)
	public String index(){
		return "/user/index.jsp";
	}

//	@RequestMapping(value = "/create",method = RequestMethod.POST)
//	@ResponseBody
//	public Map create(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request){
//		HashMap map = new HashMap();
//
//		String path = request.getSession().getServletContext().getRealPath("/avatar");
//		String fileName = file.getOriginalFilename();
//		//String fileName = new Date().getTime()+".jpg";
//		//path:保存路径，fileNmae：文件名称
//		System.out.println("保存路径为"+path);
//		File targetFile = new File(path, fileName);
//		if(!targetFile.exists()){
//			targetFile.mkdirs();
//		}
//		try {
//			//写入文件
//			file.transferTo(targetFile);
//		} catch (Exception e) {
//			e.printStackTrace();
//			System.out.println("保存失败");
//		}
//
//		User user = new User();
//		user.setAvatar(request.getContextPath()+"/image/"+fileName);
//		String stamp = DateUtil.getStamp();
//		user.setCreate_time(stamp);
//		user.setUpdate_time(stamp);
//		Integer integer = userService.addUser(user);
//
//		if(integer == 1){
//			map.put("result","success");
//		}else{
//			map.put("result","fail");
//		}
//		return map;
//	}

	@RequestMapping(value = "/uploadImg",method = RequestMethod.POST)
	@ResponseBody
	public Map create(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request) throws Exception {
		// 获取文件名
		String fileName = file.getOriginalFilename();

		// 自定义的文件名称
		String trueFileName=String.valueOf(System.currentTimeMillis())+fileName;
		// 获取图片后缀
//		String extName = oriName.substring(oriName.lastIndexOf("."));

		String path = request.getSession().getServletContext().getRealPath("avatar");
		System.out.println("bbbbbbbbbbbbbb"+fileName);
		System.out.println("cccccccccccccc"+path);

		// 开始上传保存到指定位置
		file.transferTo(new File(path+"/"+trueFileName));

		Map map = new HashMap();
		map.put("result","success");
		map.put("imgSrc",path+"/"+trueFileName);
		return map;
	}

	@RequestMapping(value = "/update",method = RequestMethod.POST)
	@ResponseBody
	public Map update(User user){
		user.setUpdate_time(DateUtil.getStamp());
		HashMap map = new HashMap();
		try {
			userService.updateUser(user);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result","fail");
		}
		map.put("result","success");
		return map;
	}

	@RequestMapping(value = "allocationList",method = RequestMethod.POST)
	@ResponseBody
	public List allcationList(){
		List<Role> roles = roleService.selectByList();
		HashMap<String, Object> map = new HashMap();
		List list = new ArrayList();
		for(int i = 0;i<roles.size();i++){
			map.put("id",roles.get(i).getId());
			map.put("text",roles.get(i).getTitle());
			list.add(map);
		}
		return list;
	}

	@RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
	@ResponseBody
	public Map delete(@PathVariable("ids") int[] ids){
		userService.deleteByIds(ids);
		HashMap map = null;
		try {
			map = new HashMap();
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result","fail");
		}
		map.put("result","success");
		return map;
	}

	//@RequestParam(required = false, defaultValue = "0", value = "offset")
	//@RequestParam(required = false, defaultValue = "10", value = "limit")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Map list(@RequestParam int offset, @RequestParam int limit) {
		int a = (offset/limit)+1;
		PageHelper.startPage(a,limit);
		List<User> userList = userService.selectByList();
		PageInfo<User> p=new PageInfo<User>(userList);
		Map<String, Object> result = new HashMap();
		result.put("rows", p.getList());
		result.put("total", p.getTotal());
		return result;
	}

	@RequestMapping(value = "/uptUserRole", method = RequestMethod.GET)
	@ResponseBody
	public Object uptUserRole(@RequestParam int userId, @RequestParam int roleId){
		Integer insert = 0;
		userService.delRoleByUserId(userId);
		insert = userService.insertUserRole(userId, roleId);
		HashMap map = new HashMap();
		if(insert == 1){
			map.put("result","success");
		}else{
			map.put("result","fail");
		}
		return map;
	}
}
